package com.cskaoyan.controller.backstagecontroller;

import com.cskaoyan.bean.data.BaseData;
import com.cskaoyan.bean.param.AdminParam;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.pojo.Admin;
import com.cskaoyan.bean.pojo.Log;
import com.cskaoyan.bean.pojo.Role;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.RoleOptionsVo;
import com.cskaoyan.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ：dailin
 * @date ：
 * @description：系统管理
 */


@RestController
@RequestMapping("admin/")
public class SystemManagementController {

    @Autowired
    AdminService adminService;

    //显示所有管理员信息
    @RequestMapping("admin/list")
    public BaseRespVo selectAll(AdminParam adminParam) {
        BaseData<Admin> baseData = adminService.selectAllAdmin(adminParam);
        return BaseRespVo.ok(baseData);
    }

    //显示管理员的权限
    @RequestMapping("role/options")
    public BaseRespVo showResult() {
        List<RoleOptionsVo> roles = adminService.selectAllRole1();
        return BaseRespVo.ok(roles);
    }

    //新增管理员
    @RequestMapping("admin/create")
    public BaseRespVo createAdmin(@RequestBody Admin admin) {
        Integer effectRow = adminService.addAdmin(admin);
        if (effectRow == 1) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail("添加失败");
    }

    //删除管理员
    @RequestMapping("admin/delete")
    public BaseRespVo deleteAdmin(@RequestBody Admin admin) {
        int effectRows = adminService.deleteAdmin(admin);
        if (effectRows == 1) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail("删除失败");
    }

    //修改管理员
    @RequestMapping("admin/update")
    public BaseRespVo updateAdmin(@RequestBody Admin admin) {
        Admin updateAfterAdmin = adminService.updateAdmin(admin);
        return BaseRespVo.ok(updateAfterAdmin);
    }

    //操作日志
    @RequestMapping("log/list")
    public BaseRespVo logList(AdminParam adminParam) {
        BaseData<Log> logBaseData = adminService.logList(adminParam);
        return BaseRespVo.ok(logBaseData);
    }

}
